package learning.pcfg.inference;

import java.util.ArrayList;
import java.util.List;

public class Parse {
	
	public int production;
	public Parse[] childParses;
	public Region region;
	public float score;
	
	public Parse(int production, Parse[] childParses, Region region) {
		this.production = production;
		this.childParses = childParses;
		this.region = region;
	}
	
	public List<Parse> getTreeNodes() {
		List<Parse> list = new ArrayList<Parse>();
		getTreeNodes(this, list);
		return list;
	}
	
	public static void getTreeNodes(Parse parse, List<Parse> list) {
		list.add(parse);
		if (parse.childParses != null)
			for (int i=0; i < parse.childParses.length; i++)
				getTreeNodes(parse.childParses[i], list);		
	}
		
	public List<Parse> getTerminalNodes() {
		List<Parse> list = new ArrayList<Parse>();
		getTerminalNodes(this, list);
		return list;
	}

	public static void getTerminalNodes(Parse parse, List<Parse> list) {
		if (parse.childParses != null)
			for (int i=0; i < parse.childParses.length; i++)
				getTerminalNodes(parse.childParses[i], list);
		else
			list.add(parse);
	}
}
